package com.dd.ent.car.ui.home.templet.choice;

import android.view.View;

import com.dd.ent.car.AppContext;
import com.dd.ent.car.common.bean.base.PageBean;
import com.dd.ent.car.common.bean.base.ResultBean;
import com.dd.ent.car.common.bean.choice.Choice;
import com.dd.ent.car.common.config.UIConfig;
import com.dd.ent.car.common.player.MediaPlayerManager;
import com.dd.ent.car.service.home.choice.ChoiceService;
import com.dd.ent.car.ui.adapter.base.BaseListAdapter;
import com.dd.ent.car.ui.adapter.general.ChoiceAdapter;
import com.dd.ent.car.ui.home.templet.HomeTempletListFragment;
import com.dd.ent.car.ui.widget.ViewNewsHeader;
import com.google.gson.reflect.TypeToken;
import com.pp.ent.base.log.MLog;
import com.pp.ent.core.manage.UIHandler;
import com.pp.ent.view.annotation.Inject;

import java.lang.reflect.Type;


/**
 * 精选界面
 */
public class ChoiceFragment extends HomeTempletListFragment<Choice> {
    private final static String TAG = "ChoiceFragment";


    @Inject
    private ChoiceService choiceService;

    private boolean isFirst = true;
    private ViewNewsHeader mHeaderView;
    private int startPos = 1;
    private boolean reset = false;

    @Override
    protected void initWidget(View root) {
        super.initWidget(root);
    }

    @Override
    protected void initData() {
        ONE_PAGE_MAX_SIZE = 4;
        super.initData();
        requestData();

    }

    @Override
    public void onRefreshing() {
        startPos = 1;
        reset = false;
        ONE_PAGE_MAX_SIZE = 4;
        super.onRefreshing();
        if (!isFirst)
            getBannerList();
    }

    @Override
    protected void requestData() {
        super.requestData();
        choiceService.getChociceByPage(startPos, ONE_PAGE_MAX_SIZE);
    }

    @Override
    protected BaseListAdapter<Choice> getListAdapter() {
        return new ChoiceAdapter(this);
    }

    @Override
    protected Type getType() {
        return new TypeToken<ResultBean<PageBean<Choice>>>() {
        }.getType();
    }

    @Override
    protected void onRequestFinish() {
        super.onRequestFinish();
        isFirst = false;
    }

    @Override
    protected void setListData(ResultBean<PageBean<Choice>> resultBean) {
        // ((ChoiceAdapter) mAdapter).setSystemTime(resultBean.getTime());
        super.setListData(resultBean);
        if (resultBean.getResult().getItems().size() < ONE_PAGE_MAX_SIZE * 2){
            if (!reset){
                startPos = startPos +1;
                ONE_PAGE_MAX_SIZE = 8;
                reset = true;
            }
        }else {
            startPos = mAdapter.getCount() / (ONE_PAGE_MAX_SIZE * 2) + 1;
        }
    }

    @Override
    protected String getPlayTag() {
        return MediaPlayerManager.PlayerTag.HOME;
    }

    private void getBannerList() {
    }


    @Override
    public void onPause() {
        super.onPause();
        MediaPlayerManager.getInstance().releaseMediaPlayer(MediaPlayerManager.PlayerTag.HOME);
    }

    @Override
    public void onPageChanged(int number) {
        super.onPageChanged(number);
        MediaPlayerManager.getInstance().releaseMediaPlayer(MediaPlayerManager.PlayerTag.HOME);
    }

    @UIHandler(UIConfig.QUERY_ALL_CHOICE_SUCCESS)
    public void getListChocieRes(String json) {
        MLog.debug(TAG, "getListChocieRes json=" + json);
        ResultBean<PageBean<Choice>> resultBean = AppContext.createGson().fromJson(json, getType());
        setListData(resultBean);
        onRequestFinish();

    }

    @UIHandler(UIConfig.ADD_VIDEO_INFO_TO_SERVER_SUCCESS)
    public void addVideoInfoServerResponse() {
        onRefreshing();
    }

    @UIHandler(UIConfig.ADD_NOTE_INFO_TO_SERVER_SUCCESS)
    public void addNoteInfoServerSuccessResponse() {
        onRefreshing();
    }

}
